package com.leiqichao.backend.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import nl.basjes.parse.useragent.UserAgent;
import nl.basjes.parse.useragent.UserAgentAnalyzer;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class UserAgentParser {

    private final UserAgentAnalyzer uaa;
    private final ObjectMapper objectMapper;

    // 通过构造器注入依赖
    public UserAgentParser(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
        this.uaa = UserAgentAnalyzer
                .newBuilder()
                .hideMatcherLoadStats()
                .withCache(10000)
                .build();
    }

    /**
     * 解析User-Agent并返回所有信息的JSON字符串（中文表示）
     */
    public String parseToJson(String userAgentString) throws JsonProcessingException {
        UserAgent agent = uaa.parse(userAgentString);
        return objectMapper.writeValueAsString(extractAllFieldsInChinese(agent));
    }

    /**
     * 解析User-Agent并返回结构化对象（中文表示）
     */
    public ParsedUserAgent parse(String userAgentString) {
        UserAgent agent = uaa.parse(userAgentString);
        return new ParsedUserAgent(extractAllFieldsInChinese(agent));
    }

    /**
     * 提取所有可用字段并转换为中文表示
     */
    private Map<String, String> extractAllFieldsInChinese(UserAgent agent) {
        Map<String, String> result = new HashMap<>();

        // 设备信息
        result.put("设备类型", agent.getValue("DeviceClass"));
        result.put("设备名称", agent.getValue("DeviceName"));
        result.put("设备品牌", agent.getValue("DeviceBrand"));
        result.put("设备CPU", agent.getValue("DeviceCpu"));

        // 操作系统信息
        result.put("操作系统", agent.getValue("OperatingSystem"));
        result.put("操作系统名称", agent.getValue("OperatingSystemName"));
        result.put("操作系统版本", agent.getValue("OperatingSystemVersion"));
        result.put("操作系统名称版本", agent.getValue("OperatingSystemNameVersion"));

        // 浏览器信息
        result.put("浏览器名称", agent.getValue("AgentName"));
        result.put("浏览器版本", agent.getValue("AgentVersion"));
        result.put("浏览器名称版本", agent.getValue("AgentNameVersion"));
        result.put("浏览器主版本", agent.getValue("AgentVersionMajor"));

        // 布局引擎
        result.put("布局引擎名称", agent.getValue("LayoutEngineName"));
        result.put("布局引擎版本", agent.getValue("LayoutEngineVersion"));
        result.put("布局引擎名称版本", agent.getValue("LayoutEngineNameVersion"));

        return result;
    }

    @Data
    public static class ParsedUserAgent {
        private final Map<String, String> fields;

        // 提供便捷访问方法
        public String getDeviceType() {
            return fields.get("设备类型");
        }

        public String getOsInfo() {
            return fields.get("操作系统名称版本");
        }

        public String getBrowserInfo() {
            return fields.get("浏览器名称版本");
        }

        // 获取原始字段Map
        public Map<String, String> getAllFields() {
            return new HashMap<>(fields);
        }
    }
}